package com.itheima.controller.pms;

import com.itheima.core.PageInfo;
import com.itheima.core.R;
import com.itheima.pojo.pms.PmsProductAttributeCategory;
import com.itheima.pojo.pms.vo.ProductAttributeCategoryListWithAttrVo;
import com.itheima.service.pms.PmsProductAttributeCategoryService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品属性分类管理Controller
 */
@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {
    @DubboReference
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;


    /**
     * 添加商品属性分类
     * @param name
     * @return
     */
    @PostMapping("create")
    public R create(@RequestParam String name) {
        PmsProductAttributeCategory pmsProductAttributeCategory=new PmsProductAttributeCategory();
        pmsProductAttributeCategory.setName(name);
        pmsProductAttributeCategoryService.save(pmsProductAttributeCategory);
        return R.success();
    }

    /**
     * 删除单个商品属性分类
     * @param id
     * @return
     */
    @GetMapping("delete/{id}")
    public R delete(@PathVariable Long id) {
        pmsProductAttributeCategoryService.removeById(id);
        return R.success();
    }

    /**
     * 分页获取所有商品属性分类
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    public R List(@RequestParam(defaultValue ="1")Integer pageNum,@RequestParam(defaultValue ="5")Integer pageSize){
        PageInfo list = pmsProductAttributeCategoryService.listBrand( pageNum, pageSize);
        return R.success("success",list);
    }

    /**
     * 获取所有商品属性分类及其下属性
     * @return
     */
    @GetMapping("/list/withAttr")
    public R getListWithAttr() {
        List<ProductAttributeCategoryListWithAttrVo> list=pmsProductAttributeCategoryService.getListWithAttr();
        return R.success(list);
    }

    /**
     * 修改商品属性分类
     * @param id
     * @param name
     * @return
     */
    @PostMapping("update/{id}")
    public R update(@PathVariable Long id, @RequestParam String name) {
        PmsProductAttributeCategory pmsProductAttributeCategory=new PmsProductAttributeCategory();
        pmsProductAttributeCategory.setName(name);
        pmsProductAttributeCategory.setId(id);
        pmsProductAttributeCategoryService.updateById(pmsProductAttributeCategory);
        return R.success();
    }

    /**
     * 获取单个商品属性分类信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R getById(@PathVariable Long id) {
        return R.success( pmsProductAttributeCategoryService.getById(id));
    }
}
